//index.js
//获取应用实例
const util=require("../../utils/util.js");
const app = getApp()
const baseImgUrl=util.baseImgUrl;
const jumpurl = util.jumpurl;
var page=1;


Page({
  data: {
    indicatorDots: false,//是否显示轮播图的面板指示点
    indicatorDots2:false,

    autoplay: true,//轮播图是否自动播放

    interval: 5000,
    duration: 1000,
    duration2:1000,

    showGrey:false,
    scanPic:baseImgUrl+"scan_home.png",
    searchPic:baseImgUrl+"search_home.png",
    wb_bgImg: baseImgUrl +"wb_bgImg.png",

    barPic:baseImgUrl+"index_bar.png",

  },
  //跳转到了解优当课堂
  know:function(){

  },
  //跳转至搜索页面
  gosearch:function(){

  },
  //进入学堂
  goschool:function(e){
    console.log(e); 
    var index=e.currentTarget.dataset.index;
    if(index==3){
      wx.navigateTo({
        url: '/pages/tisheng/tisheng?classid=' + e.currentTarget.dataset.id,
      })
    }else{
      wx.navigateTo({
        url: '/pages/school/school?classid=' + e.currentTarget.dataset.id,
      })
    }
  },

  //事件处理函数
  goDetail: function(e) {
      var that=this;
      var newid=e.currentTarget.dataset.id;
      wx.request({
        url: jumpurl + 'my/get_vip',
        data: {
          openid: app.globalData.openid,
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          if (res.data.vip_level == 0) {
            that.setData({
              showGrey: true,
            })
          } else {
            wx.navigateTo({
              url: '/pages/audioDetail/audioDetail?newid=' + newid,
            })
          }
        },
      })
   },

  goDetail2:function(e){
    var that = this;
    var newid = e.currentTarget.dataset.id;
    var index_now=e.currentTarget.dataset.index;
    that.data.newestlist[index_now].hasread=1;
    that.setData({
      newestlist: that.data.newestlist
    })
    wx.request({
      url: jumpurl + 'my/get_vip',
      data: {
        openid: app.globalData.openid,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
          if(res.data.vip_level  == 0){
            that.setData({
              showGrey: true,
            })
          }else{
            wx.navigateTo({
              url: '/pages/audioDetail/audioDetail?newid=' + newid,
            })
          }
      },
    })
    
  },
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        console.log("系统"+res.windowHeight);
      },
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }

    //定位
    wx.getLocation({
      success: function(res) {
        console.log("定位信息");
        console.log(res);
        var latitude=res.latitude;
        var longitude=res.longitude;
      },fail:function(err){
        console.log("用户拒绝定位"+err);
      },complete:function(res){
          console.log("定位完成"+res);
      }
    })


    app.getOpenid().then(function (res) {
      if (res.status == 200) {
        app.globalData.openid = wx.getStorageSync('openid');
        app.globalData.shopid = wx.getStorageSync('shopid');
        that.setData({
          openid: wx.getStorageSync('openid')
        })

        if (options.open == null) {
          var open = 0;
        } else {
          var open = options.open;
        }


        wx.request({
          url: jumpurl + 'index/index',
          data: {
            open: open,
            openid: app.globalData.openid,
            shopid: app.globalData.shopid,
          },
          method: "POST",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            console.log(res);
            that.setData({
              s_imgUrls: res.data.s_imgUrls,
              navlist: res.data.navlist,
              newestlist: res.data.newestlist,
              newestlist2: res.data.newestlist2,
              wbText: res.data.wbText,
              first_class_id: res.data.first_class_id,
            });
          },
        })
      }
    });
  },

//学习资源点击更多
  morein:function(){
    var first_class_id = this.data.first_class_id;
    wx.navigateTo({
      url: '/pages/school/school?classid=' + first_class_id,
    })
  },

  ///我再想想
  wait: function () {
    var that = this;
    that.setData({
      showGrey:false
    })
  },
  //开通会员
  member: function () {
    var that = this;
    that.setData({
      showGrey: false
    })
    wx.navigateTo({
      url: '/pages/kaitong/kaitong',
    })
  },
    
 
  //到底
  onReachBottom:function(){   
      util.loading();
      //请求接口
      var that=this;
      var newestlist2=that.data.newestlist2;
      page = page+1;
      wx.request({
        url: jumpurl + 'index/video_page',
        data: {
          page: page,
        },
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          console.log(res);
          if (page <= res.data.zongye){
            newestlist2 = newestlist2.concat(res.data.newestlist2);
            that.setData({
              newestlist2: newestlist2
            })
          }
          if (page > res.data.zongye){
            wx.showToast({
              icon: "none",
              title: '没有更多数据了',
            })
          }
        },
      })
  },

  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})

